package com.zzq.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzq.domain.dto.SmileCouponDTO;
import com.zzq.domain.entity.SmileCoupon;
import com.zzq.domain.search.CouponSearchParamet;
import com.zzq.enumerate.CouponStatusEnum;
import com.zzq.mapper.SmileCouponMapper;
import com.zzq.service.SmileCouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzq.util.ObjectUtils;
import com.zzq.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小布
 * @since 2023-10-26
 */
@Service
public class SmileCouponServiceImpl extends ServiceImpl<SmileCouponMapper, SmileCoupon> implements SmileCouponService {

    @Autowired
    SmileCouponMapper couponMapper;
    @Override
    public List<SmileCouponDTO> getCouponDTOList(CouponSearchParamet searchParamet) {
        List<SmileCouponDTO> couponDTOList = couponMapper.getCouponDTOList(searchParamet);
        return couponDTOList;
    }

    @Override
    public SmileCouponDTO getCouponDTObyNo(String couponNo) {
        CouponSearchParamet searchParamet=new CouponSearchParamet();
        searchParamet.setCouponNo(couponNo);
        List<SmileCouponDTO> couponDTOList = couponMapper.getCouponDTOList(searchParamet);
        if (ObjectUtils.isListSole(couponDTOList)) {
            return  couponDTOList.get(0);
        }
        return null;
    }

    @Override
    public SmileCoupon addCoupon(Long clientId, String clientNo, Long couponRuleId, Date expirationTime) {
        SmileCoupon coupon=new SmileCoupon();
        coupon.setCouponNo("PJ-"+ UUIDUtil.generateCustomString(6));
        coupon.setClientId(clientId);
        coupon.setClientNo(clientNo);
        coupon.setCouponRuleId(couponRuleId);
        coupon.setExpirationTime(expirationTime);
        coupon.setCouponStatus(CouponStatusEnum.UNUSED.getName());
        couponMapper.insert(coupon);
        return coupon;
    }

    @Override
    public SmileCoupon updCouponStatus(String couponNo, String couponStatus) {
        LambdaUpdateWrapper<SmileCoupon> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(SmileCoupon::getCouponNo,couponNo);
        updateWrapper.set(SmileCoupon::getCouponStatus,couponStatus);
        couponMapper.update(null,updateWrapper);
        return null;
    }

    @Override
    public Integer getCouponNum(Long clientId) {
        CouponSearchParamet searchParamet=new CouponSearchParamet();
        searchParamet.setClientId(clientId);
        searchParamet.setCouponStatus(CouponStatusEnum.UNUSED.getName());
        List<SmileCouponDTO> couponDTOList = couponMapper.getCouponDTOList(searchParamet);
        int size = couponDTOList.size();
        return size;
    }
}
